package com.adminpanel.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.adminpanel.modelbean.UserBean;

public class AuthorizeInterceptor extends HandlerInterceptorAdapter {

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {

		boolean handlerOk = super.preHandle(request, response, handler);
		if (handlerOk) {

			String url = request.getRequestURL().toString();
			if (url.endsWith("login")||url.endsWith("register")||url.endsWith("uploadfiles")) {

				return true;
			}

			HttpSession session = request.getSession();
			UserBean user = (UserBean) session.getAttribute("loginuser");
			if(user == null){
				response.sendRedirect("/adminpanel/login");
				
			}
			return true;

		}
		return false;
	}

}
